package org.dromara.iot.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.iot.domain.bo.IotUpkeepInfoBo;
import org.dromara.iot.domain.vo.IotUpkeepInfoVo;
import org.dromara.iot.service.IIotUpkeepInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 电梯保养信息
 *
 * @author lsl
 * @date 2024-06-14
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/iot/upkeepInfo")
public class IotUpkeepInfoController extends BaseController {

    private final IIotUpkeepInfoService iotUpkeepInfoService;

    /**
     * 查询电梯保养信息列表
     */
    @SaCheckPermission("iot:upkeepInfo:list")
    @GetMapping("/list")
    public TableDataInfo<IotUpkeepInfoVo> list(IotUpkeepInfoBo bo, PageQuery pageQuery) {
        return iotUpkeepInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出电梯保养信息列表
     */
    @SaCheckPermission("iot:upkeepInfo:export")
    @Log(title = "电梯保养信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(IotUpkeepInfoBo bo, HttpServletResponse response) {
        List<IotUpkeepInfoVo> list = iotUpkeepInfoService.queryList(bo);
        ExcelUtil.exportExcel(list, "电梯保养信息", IotUpkeepInfoVo.class, response);
    }

    /**
     * 获取电梯保养信息详细信息
     *
     * @param upkeepId 主键
     */
    @SaCheckPermission("iot:upkeepInfo:query")
    @GetMapping("/{upkeepId}")
    public R<IotUpkeepInfoVo> getInfo(@NotNull(message = "主键不能为空")
                                      @PathVariable Long upkeepId) {
        return R.ok(iotUpkeepInfoService.queryById(upkeepId));
    }

    /**
     * 新增电梯保养信息
     */
    @SaCheckPermission("iot:upkeepInfo:add")
    @Log(title = "电梯保养信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody IotUpkeepInfoBo bo) {
        return toAjax(iotUpkeepInfoService.insertByBo(bo));
    }

    /**
     * 修改电梯保养信息
     */
    @SaCheckPermission("iot:upkeepInfo:edit")
    @Log(title = "电梯保养信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody IotUpkeepInfoBo bo) {
        return toAjax(iotUpkeepInfoService.updateByBo(bo));
    }

    /**
     * 删除电梯保养信息
     *
     * @param upkeepIds 主键串
     */
    @SaCheckPermission("iot:upkeepInfo:remove")
    @Log(title = "电梯保养信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{upkeepIds}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] upkeepIds) {
        return toAjax(iotUpkeepInfoService.deleteWithValidByIds(List.of(upkeepIds), true));
    }

    /**
     * 开始保养电梯
     *
     * @param upkeepId 主键
     */
    @GetMapping("startUpkeep/{upkeepId}")
    public R<Void> startUpkeep(@NotNull(message = "主键不能为空")
                               @PathVariable Long upkeepId) {
        return toAjax(iotUpkeepInfoService.startUpkeep(upkeepId));
    }

    /**
     * 进行保养项目清单页面详情接口
     *
     * @param upkeepId    主键
     * @param projectType 项目类型
     * @return 项目详情
     */
    @GetMapping("projectDetail")
    public R<IotUpkeepInfoVo> projectDetail(@NotNull(message = "主键不能为空") Long upkeepId, String projectType) {
        return R.ok(iotUpkeepInfoService.projectDetail(upkeepId, projectType));
    }


    /**
     * 提交保养审核信息
     *
     * @param upkeepInfo 保养信息
     * @return 是否成功
     */
    @PostMapping("/submitUpkeepAuditInfo")
    @RepeatSubmit()
    public R<Void> submitUpkeepAuditInfo(@RequestBody IotUpkeepInfoBo upkeepInfo) {
        return toAjax(iotUpkeepInfoService.submitUpkeepAuditInfo(upkeepInfo));
    }

    @SaIgnore
    @GetMapping("/saveUpkeepBusinessInfo")
    public R<Void> saveUpkeepBusinessInfo() {
        iotUpkeepInfoService.saveUpkeepBusinessInfo();
        return R.ok();
    }
}